package Game.entity;

import Game.utils.CollisionUtils;
import Game.utils.Direction;
import Game.utils.Meta;
import Game.utils.SoundUtils;

import java.io.IOException;

public class Tank extends Element {

    int surplus = 0;
    public int direction = 16;

    private int num = 16;

    public Direction default_dir = Direction.UP;
    public Direction unmoved_dir;

    public Tank() {

    }

    public Tank(int x, int y) {
        this.x = x;
        this.y = y;
        super.path = "15-Tankwar\\images\\tank_u.gif";
        getSize();

    }

    public void move(Direction d) {

        if (unmoved_dir == d && unmoved_dir != null) {
//          停止前走完留白距离
            switch (d) {
                case UP:
                    y -= surplus;
                    break;

                case DOWN:
                    y += surplus;
                    break;

                case LEFT:
                    x -= surplus;
                    break;

                case RIGHT:
                    x += surplus;
                    break;
            }
            return;
        }
        if (d == this.default_dir) {
            switch (d) {
                case UP:
                    y -= direction;
                    break;
                case DOWN:
                    y += direction;
                    break;
                case LEFT:
                    x -= direction;
                    break;
                case RIGHT:
                    x += direction;
                    break;
            }
        } else {
//          转向不要修改坐标！！！，被坑惨了
            switch (d) {
                case UP:
                    path = "15-Tankwar\\images\\tank_u.gif";
//                    y -= direction;
                    break;
                case DOWN:
                    path = "15-Tankwar\\images\\tank_d.gif";
//                    y += direction;
                    break;
                case LEFT:
                    path = "15-Tankwar\\images\\tank_l.gif";
//                    x -= direction;
                    break;
                case RIGHT:
                    path = "15-Tankwar\\images\\tank_r.gif";
//                    x += direction;
                    break;
            }
            this.default_dir = d;

        }

//      防止越界
        if (x < 0) x = 0;

        if (x > Meta.WIDTH - this.width) {
            x = Meta.WIDTH - this.width;
        }
        if (y < 0) y = 0;

        if (y > Meta.HEIGHT - this.height) {
            y = Meta.HEIGHT - this.height;
        }
    }

    long gap = 500;
    long lastTime;

    public Bullet fire() {

        long currentTime = System.currentTimeMillis();
        if (currentTime - lastTime >= gap) {
            lastTime = currentTime;
            try {
                SoundUtils.play("15-Tankwar\\audio\\fire.wav");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return new Bullet(x, y, width, height, default_dir);
        } else {
            return null;
        }

    }
/*  Version1,碰撞后让坦克归位，有问题，坦克有明显回弹动作
//        public boolean isCollide(Element e) {
//            //获取坦克的宽高坐标
//            int x1 = this.x;
//            int y1 = this.y;
//            int w1 = this.width;
//            int h1 = this.height;
//
//            //获取墙体的宽高坐标
//            int x2 = e.x;
//            int y2 = e.y;
//            int w2 = e.width;
//            int h2 = e.height;
//
//            boolean flag = CollisionUtils.isCollisionWithRect(x1, y1, w1, h1, x2, y2, w2, h2);
//
//            if (flag) {
//                //撞墙version1:修正坦克的坐标
//                switch (default_dir) {
//                    case UP:
//                        this.y = y2 + h2;
//                        break;
//                    case DOWN:
//                        this.y = y2 - h1;
//                        break;
//                    case LEFT:
//                        this.x = x2 + w2;
//                        break;
//                    case RIGHT:
//                        this.x = x2 - w1;
//                        break;
//                }
//            }
//            return flag;
//        }
 */

    public boolean isCollide(Element e) {
        //获取坦克的宽高坐标
        int x1 = this.x;
        int y1 = this.y;
        int w1 = this.width;
        int h1 = this.height;

        switch (default_dir) {
            case UP:
                y1 -= num;
                break;

            case DOWN:
                y1 += num;
                break;

            case LEFT:
                x1 -= num;
                break;

            case RIGHT:
                x1 += num;
                break;
        }

        //获取墙体的宽高坐标
        int x2 = e.x;
        int y2 = e.y;
        int w2 = e.width;
        int h2 = e.height;

        boolean flag = CollisionUtils.isCollisionWithRect(x1, y1, w1, h1, x2, y2, w2, h2);

//        Version2，当步长与格子大小不是整数倍时会出现留白问题
//        if (flag) {
//            unmoved_dir = default_dir;
//        } else {
//            unmoved_dir = null;
//        }
//        return flag;
//    }

//      Version3，最优解
        if (flag) {
            unmoved_dir = default_dir;

            switch (default_dir) {
                case UP:
                    surplus = super.y - y2 - h2;
                    break;
                case DOWN:
                    surplus = y2 - super.y - h1;
                    break;
                case LEFT:
                    surplus = super.x - x2 - w2;
                    break;
                case RIGHT:
                    surplus = x2 - super.x - w1;
                    break;
            }

        }else {
            unmoved_dir = null;
        }
        return flag;
    }
}